package com.nian.search.solr.controller;

import com.nian.common.model.ServerResponse;
import com.nian.search.solr.model.SolrItem;
import com.nian.search.solr.service.ISolrItemService;
import com.nian.search.solr.vo.SolrItemVO;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.SolrParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.repository.Query;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用途：
 * 开发：zhangmj
 * 日期：2020/4/14 0014 11:44
 */
@RestController
@RequestMapping("solr/test")
public class SolrItemController {

    @Autowired
    private ISolrItemService iSolrItemService;

    @Autowired
    private SolrClient solrClient;

    @PostMapping("listPage")
    public ServerResponse listPage(@RequestBody SolrItemVO query){
        PageRequest page = PageRequest.of(query.getPage().getPageNum(),query.getPage().getPageSize());
        Page<SolrItem> info =  iSolrItemService.listPage(page);
        return ServerResponse.createBySuccess(info);
    }

    @PostMapping("search")
    public ServerResponse search(@RequestBody SolrItemVO query){
        Page<SolrItem> info =  iSolrItemService.search(query);
        return ServerResponse.createBySuccess(info);
    }

    @PostMapping("findAll")
    public ServerResponse findAll(){
        Iterable<SolrItem> all = iSolrItemService.findAll();
        return ServerResponse.createBySuccess(all);
    }
    @PostMapping("get/{id}")
    public ServerResponse findById(@PathVariable("id") String id){
        SolrItem info = iSolrItemService.findById(id);
        return ServerResponse.createBySuccess(info);
    }

    @PostMapping("delete/{id}")
    public ServerResponse delete(@PathVariable("id") String id){
        iSolrItemService.deleteById(id);
        return ServerResponse.createBySuccess();
    }

    @PostMapping("save")
    public ServerResponse save(@RequestBody SolrItem model){
        SolrItem info = iSolrItemService.save(model);
        return ServerResponse.createBySuccess(info);
    }

    @PostMapping("saveAll")
    public ServerResponse saveAll(@RequestBody List<SolrItem> list){
        Iterable<SolrItem> l = iSolrItemService.saveAll(list);
        return ServerResponse.createBySuccess(l);
    }

    @PostMapping("saveAllTest")
    public ServerResponse saveAll() throws IOException, SolrServerException {

        List<SolrItem> list = new ArrayList<>();
        list.add(new SolrItem(1L, "小米手机7", "手机", "小米", 1999.00, "http://l27.0.0.1/10001.jpg"));
        list.add(new SolrItem(2L, "坚果手机R1", "手机", "锤子", 1999.00, "http://l27.0.0.1/10002.jpg"));
        list.add(new SolrItem(3L, "华为META10", "手机", "华为", 1299.00, "http://l27.0.0.1/10003.jpg"));
        list.add(new SolrItem(4L, "小米Mix2S", "手机", "小米", 1099.00, "http://l27.0.0.1/10004.jpg"));
        list.add(new SolrItem(5L, "荣耀V10", "手机", "华为", 1199.00, "http://l27.0.0.1/10005.jpg"));
        Iterable<SolrItem> l = iSolrItemService.saveAll(list);
        return ServerResponse.createBySuccess(l);
    }
}
